<template>
  <section>
    <el-row style="display: flex;align-items: center;">
      <el-col :span="6">
        <el-input size="mini" placeholder="请输入关键字"  clearable v-model="searchKey" @keydown.native.prevent.enter="searchBtn" ></el-input>
      </el-col>
      <el-col :span="4">
        <i class="el-icon-search" @click="searchBtn" style="margin-left:10px" ></i>
      </el-col>
      <el-col :span="12" >
        <el-button size="mini" @click="addProperties" style="float: right" type="primary">新建活动属性</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="propertiesList" stripe v-loading="loading">
        <el-table-column type="index" label="序号"  width="80px"></el-table-column>
        <el-table-column prop="title" label="名称" width="150px">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.name"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column label="编码" width="150px">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.code"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.code}}</span>
          </template>
        </el-table-column>
        <el-table-column label="等级">
          <template slot-scope="scope">
            <span  v-show="!scope.row.edit">{{scope.row.activityPropertiesLevel.name}}</span>
            <el-select v-model="scope.row.levelId" placeholder="选择等级" size="mini" @change="selLevelId(scope.row)" v-show="scope.row.edit">
              <el-option v-for="level in levelList" :value="level.id" :key="level.id" :label="level.name"></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="优先级">
          <template slot-scope="scope">
            <span v-show="!scope.row.edit">{{scope.row.priority}}</span>
            <span v-show="scope.row.edit">
              {{scope.row.activityPropertiesLevel.min}} ≤
              <el-input  size="mini" type="number" v-model="scope.row.priority" style="width:80px" ></el-input>
              ≤ {{scope.row.activityPropertiesLevel.max}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="值类型" width="150px">
          <template slot-scope="scope">
            <el-row  v-for="type in valTypes" :key="type.code" >
              <el-radio v-model="scope.row.data.valType" :label="type.code" >{{type.name}}</el-radio>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="模板内容">
          <template slot-scope="scope">
            <el-button @click='editData(scope)' size="small">设置</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="small" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
            <el-button @click='cancelBtn(scope.row)' size="small" v-if="scope.row.edit">取消</el-button>
            <el-button @click='del(scope)' size="small" v-if="!scope.row.edit">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-dialog :visible.sync="editDialog" v-if="currProperties" :title="currProperties.name">
      <model-detail :currProperties="currProperties" v-on:cancel="cancelDialog"></model-detail>
    </el-dialog>
  </section>
</template>

<script>
  import modelDetail from  './modelDetail'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler.js'
  export default {
    name: "properties2",
    components: {modelDetail},
    data() {
      return {
        propertiesList:[],
        searchKey:null,
        loading:false,
        editDialog:false,
        currProperties:null,
        levelList:null,
        valTypes:[
          {code:'blur',name:'判断是否'},
          {code:'input',name:'输入文本'},
          {code:'sel',name:'选择'},
          {code:'limit',name:'范围'}
        ]
      }
    },
    created(){
      this.getActivityPropertiesLevelList()
      //获取所有活动属性
      this.getActivityPropertiesList()
    },
    watch:{
    },
    computed:{
      ...mapGetters(['department']),
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      selLevelId(row){
        row.activityPropertiesLevel= this.levelList.find(l=>l.id === row.levelId)
      },
      getActivityPropertiesLevelList(){
        this.fetch({name:"getActivityPropertiesLevelList"})
          .then(res=>{
            this.levelList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      cancelDialog(){
        this.editDialog=false
      },
      getActivityPropertiesList(){
        this.fetch({name:"getActivityPropertiesList"})
          .then(res=>{
            console.log('活动属性列表',res)
            this.propertiesList=res.content
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      addProperties(){
        //新建一个空的，表格页面+1栏
        this.fetch({name:"createActivityProperties",data:{name:'',code:'',type:0,levelId:this.levelList[0].id,priority:this.levelList[0].min}})
          .then(res=>{
            console.log(res)
            res.edit = true
            res.levelId=this.levelList[0].id
            this.propertiesList.unshift(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      searchBtn(){
        console.log('搜索报告模板类型')
      },
      confirm(row){
        if(row.edit === undefined){
          Vue.set(row,'edit',true)//后续添加属性，需要vue联动
          Vue.set(row,'levelId',row.activityPropertiesLevel.id)//后续添加属性，需要vue联动
          return
        }
        if(row.edit){
          this.loading=true
          console.log(row)
          this.updateActivityProperties(row)
        }else {
          row.edit=true
        }
      },
      cancelBtn(row){//取消按钮
        if(row.edit === undefined){
          Vue.set(row,'edit',false)//后续添加属性，需要vue联动
        }else {
          row.edit =false
        }
      },
      del(scope){//删除按钮
        this.loading=true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"deleteActivityProperties",data:{id:scope.row.id}})
              .then(res=>{
                this.loading=false
                this.propertiesList.splice(scope.$index,1)
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updateActivityProperties(row){
        this.fetch({name:"updateActivityProperties",data:{...row}})
          .then(res=>{
            this.loading=false
            console.log(res)
            row.edit=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      editData(scope){
        this.editDialog=true
        this.currProperties=scope.row
      },
    }
  }
</script>

<style scoped>

</style>
